package com.example.leanclient;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.leanserver.IMyCount;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private ServiceConnection serviceConnection;
    private IMyCount myservice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void startCountClick(View v) {
        Log.i(TAG, "startCountClick: ");
        if (serviceConnection == null) {
            //ServiceConnection实例化，创建该对象
            Toast.makeText(this, "服务绑定 ServiceConnection实例化", Toast.LENGTH_SHORT).show();
            serviceConnection = new ServiceConnection() {
                //实现onServiceConnected方法
                @Override
                public void onServiceConnected(ComponentName name, IBinder binder) {
                    // 从连接中获取Stub对象
                    myservice = IMyCount.Stub.asInterface(binder);
                    Log.i(TAG, "onServiceConnected: ");
                }

                //实现onServiceDisconnected方法
                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.i(TAG, "onServiceDisconnected: ");
                    // 断开连接
                    myservice = null;
                }
            };
        } else {
            Toast.makeText(this, "服务已绑定", Toast.LENGTH_SHORT).show();
        }
        //绑定服务
        //2 隐式调用，在Android 5.0之后直接隐式声明Intent 启动Service引发异常, 参考：https://blog.csdn.net/l2show/article/details/47421961
        Intent intent = new Intent();
        intent.setAction("com.example.leanserver.service.action.CountService");
        intent.setPackage("com.example.leanserver");
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    public void getCountClick(View v) {
        Log.i(TAG, "getCountClick: ");
        if (myservice != null) {
            TextView tv1 = (TextView) findViewById(R.id.textview1);
            int count = 0;
            try {
                count = myservice.getcount();
            } catch (RemoteException e) {
                Log.e(TAG, "getCountClick: " + e.getMessage());
            }
            tv1.setText("当前的计数值为：" + String.valueOf(count));
        } else {
            Toast.makeText(this, "服务未启动", Toast.LENGTH_SHORT).show();
        }
    }

    public void stopCountClick(View v) {
        Log.i(TAG, "stopCountClick: ");
        // Service绑定了之后才能解绑
        if (serviceConnection != null) {
            unbindService(serviceConnection);
            serviceConnection = null;
            myservice = null;
        } else {
            Toast.makeText(this, "服务未启动", Toast.LENGTH_SHORT).show();
        }
    }
}
